﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DotNet.Random.DateTime
{
    /// <summary>
    /// 时间随机类，实现了接口IDateTimeRandService
    /// </summary>
    public class DateTimeRandService : DotNet.Random.Interface.IDateTimeRandService
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DateTimeRandService" /> class.
        /// </summary>
        /// <param name="year">The year.年随机类</param>
        /// <param name="month">The month.月随机类</param>
        /// <param name="day">The day.天随机类</param>
        /// <param name="hour">The hour.小时随机类</param>
        /// <param name="minute">The minute.分钟随机类</param>
        /// <param name="second">The second.秒随机类</param>
        /// <param name="mill">The mill.毫秒随机类</param>
        /// <exception cref="System.ArgumentNullException">除了毫秒随机类，其他随机类不能null</exception>
        public DateTimeRandService(Date.YearRandService year, Date.MonthRandService month
            , DotNet.Random.Date.DayRandService day, Time.HourRandService hour, Time.MinuteRandService minute
            , Time.SecondRandService second, Time.MillisecondRandService mill = null)
        {
            if (year == null)
            {
                throw new ArgumentNullException("year is null");
            }
            if (month == null)
            {
                throw new ArgumentNullException("month is null");
            }
            if (hour == null)
            {
                throw new ArgumentNullException("hour is null");
            }
            if (minute == null)
            {
                throw new ArgumentNullException("minute is null");
            }
            if (second == null)
            {
                throw new ArgumentNullException("second is null");
            }
            this.DateRand = new Date.DateRandService(year, month, day);
            this.TimeRand = new Time.TimeRandService(hour, minute, second, mill);
        }

        /// <summary>
        /// Gets the date rand.
        /// 日期随机类属性
        /// </summary>
        /// <value>
        /// The date rand.
        /// </value>
        protected Date.DateRandService DateRand { get; private set; }

        /// <summary>
        /// Gets the time rand.
        /// 时间随机类属性
        /// </summary>
        /// <value>
        /// The time rand.
        /// </value>
        protected Time.TimeRandService TimeRand { get; private set; }

        /// <summary>
        /// Rands this instance.
        /// 随机生成时间方法
        /// 映射到虚方法中，扩展时，可重写
        /// </summary>
        /// <returns>
        /// 返回时间类型
        /// </returns>
        public virtual System.DateTime Rand()
        {
            this.DateRand.Rand();
            this.TimeRand.Rand();

            var time = new System.DateTime(this.DateRand.Year, this.DateRand.Month,
                this.DateRand.Day, this.TimeRand.Hour, this.TimeRand.Minute,
                this.TimeRand.Second, this.TimeRand.Millisecond);

            return time;
        }
    }
}
